phpDocumentor Web Commons
[ class tree: Web Commons ] [ index: Web Commons ] [ all elements ]

Source for file model.php

Documentation is available at model.php

  1. <?php
  2. /**
  3.  * This file groups classes pertaining to the "model" part of MVC.
  4.  * 
  5.  * @author Antoine d'Otreppe de Bouvette <a.dotreppe@aspyct.org>
  6.  * @license http://www.opensource.org/licenses/mit-license.php
  7.  * @version 0.1dev
  8.  */
  9.  
  10. /**
  11.  * Generic attribute container class.
  12.  * Give an associative array to its constructor to define the existing fields.
  13.  * @since 0.1
  14.  */
  15. class Entity {
  16.     /**
  17.      * Defines and contains the existing fields.
  18.      * @var mixed[string] 
  19.      * @since 0.1
  20.      */
  21.     private $attrs;    
  22.     
  23.     public function __construct($attrs{
  24.         $this->attrs $attrs;
  25.     }
  26.     
  27.     /**
  28.      * Returns the value of the attribute $attr.
  29.      * @param string $attr 
  30.      * @throws DomainException if $attr does not exist.
  31.      * @return mixed 
  32.      * @since 0.1
  33.      */
  34.     public function __get($attr{
  35.         if (array_key_exists($attr$this->attrs))
  36.             return $this->attrs[$attr];
  37.         else
  38.             return $this->triggerAttributeError($attr);
  39.     }
  40.     
  41.     /**
  42.      * Returns the value of the attribute $attr.
  43.      * @param string $attr 
  44.      * @param mixed $value 
  45.      * @throws DomainException if $attr does not exist.
  46.      * @since 0.1
  47.      */
  48.     public function __set($attr$value{
  49.         if (array_key_exists($attr$this->attrs))
  50.             return $this->attrs[$attr$value;
  51.         else
  52.             return $this->triggerAttributeError($attr);
  53.     }
  54.     
  55.     /**
  56.      * Returns the attributes of this class in an associative array.
  57.      * @return array 
  58.      * @since 0.1
  59.      */
  60.     public function toArray({
  61.         return $this->attrs;
  62.     }
  63.     
  64.     /**
  65.      * Sets the value of attributes according to an associative array.
  66.      * No attribute will be added or deleted.
  67.      * Attributes not present in the array will be set to Null.
  68.      * 
  69.      * @param array $array 
  70.      */
  71.     public function fromArray($array{
  72.         foreach (array_keys($this->attrsas $attr{
  73.             $this->attrs[$attrarray_get_default($array$attrNull);
  74.         }
  75.     }
  76.     
  77.     /**
  78.      * Throws an exception stating that no such attribute exists.
  79.      * @param string $attr 
  80.      * @throws DomainException
  81.      * @since 0.1
  82.      */
  83.     private function triggerAttributeError($attr{
  84.         $cls new ReflectionClass($this);
  85.         throw new DomainException(
  86.             'No attribute "' .$attr'" in class ' $cls->getName());
  87.     }
  88. }

Documentation generated on Fri, 16 Jul 2010 00:48:39 +0200 by phpDocumentor 1.4.3